package com.nbcio.pm.controller;

import java.util.List;
import java.util.Arrays;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.nbcio.common.core.validate.AddGroup;
import com.nbcio.common.core.validate.EditGroup;
import com.nbcio.common.web.core.BaseController;
import com.nbcio.common.core.domain.R;
import com.nbcio.common.excel.utils.ExcelUtil;
import com.nbcio.common.mybatis.core.page.TableDataInfo;
import com.nbcio.common.mybatis.core.page.PageQuery;
import com.nbcio.pm.domain.vo.PmProjectMemberVo;
import com.nbcio.pm.domain.bo.PmProjectMemberBo;
import com.nbcio.pm.service.IPmProjectMemberService;
import com.nbcio.common.log.annotation.Log;
import com.nbcio.common.log.enums.BusinessType;
import lombok.RequiredArgsConstructor;

/**
 * 项目成员
 *
 * @author nbmade
 * @date 2025-01-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pm/project/member")
public class PmProjectMemberController extends BaseController {

    private final IPmProjectMemberService pmProjectMemberService;

    /**
     * 查询项目成员列表
     */
    @SaCheckPermission("pm:project:member:list")
    @GetMapping("/list")
    public TableDataInfo<PmProjectMemberVo> list(PmProjectMemberBo bo, PageQuery pageQuery) {
        return pmProjectMemberService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出项目成员列表
     */
    @SaCheckPermission("pm:project:member:export")
    @Log(title = "项目成员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PmProjectMemberBo bo, HttpServletResponse response) {
        List<PmProjectMemberVo> list = pmProjectMemberService.queryList(bo);
        ExcelUtil.exportExcel(list, "项目成员", PmProjectMemberVo.class, response);
    }

    /**
     * 获取项目成员详细信息
     */
    @SaCheckPermission("pm:project:member:query")
    @GetMapping(value = "/{memberId}")
    public R<PmProjectMemberVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable("memberId") Long memberId) {
        return R.ok(pmProjectMemberService.queryById(memberId));
    }

    /**
     * 新增项目成员
     */
    @SaCheckPermission("pm:project:member:add")
    @Log(title = "项目成员", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PmProjectMemberBo bo) {
        return toAjax(pmProjectMemberService.insertByBo(bo));
    }

    /**
     * 修改项目成员
     */
    @SaCheckPermission("pm:project:member:edit")
    @Log(title = "项目成员", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PmProjectMemberBo bo) {
        return toAjax(pmProjectMemberService.updateByBo(bo));
    }

    /**
     * 删除项目成员
     */
    @SaCheckPermission("pm:project:member:remove")
    @Log(title = "项目成员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{memberIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] memberIds) {
        return toAjax(pmProjectMemberService.deleteWithValidByIds(Arrays.asList(memberIds), true));
    }

    /**
     * 根据项目ID查询成员列表
     */
    @SaCheckPermission("pm:project:member:list")
    @GetMapping("/project/{projectId}")
    public R<List<PmProjectMemberVo>> getByProjectId(@PathVariable("projectId") Long projectId) {
        List<PmProjectMemberVo> list = pmProjectMemberService.queryByProjectId(projectId);
        return R.ok(list);
    }

    /**
     * 根据用户ID查询参与的项目列表
     */
    @SaCheckPermission("pm:project:member:list")
    @GetMapping("/user/{userId}")
    public R<List<PmProjectMemberVo>> getByUserId(@PathVariable("userId") Long userId) {
        List<PmProjectMemberVo> list = pmProjectMemberService.queryByUserId(userId);
        return R.ok(list);
    }
}
